<template>
  <div>
    <Modal
      v-model="modalShow"
      draggable
      scrollable
      title="质检详情"
      :styles="modalStyle"
      class="downLoadModal"
    >
      <p slot="header" class="modalHeader">质检详情</p>
      <CButtonClose slot="close" @click="closed"></CButtonClose>
      <Table
        border
        stripe
        :columns="getColumnlist"
        :data="qcdata"
        ref="selection"
        @on-selection-change="handleSelectRow"
      >
        <template slot-scope="{ row }" slot="action">
          <CButton
            v-c-tooltip="'预览'"
            title="预览"
            color="rgb(2,40,88)"
            variant="outline"
            class="btnStyle "
            @click="redaskDetails(row)"
          >
            <CIcon class="colortempstyle" name="cil-line-spacing" />
          </CButton>

          <CButton
            v-c-tooltip="'下载'"
            title="下载"
            variant="outline"
            color="rgb(2,40,88)"
            style="margin-right: 5px"
            @click="downloaddetail(row)"
            data-permission="Btn_QuaCheck_Down"
          >
            <img class="featuresstyle" src="@/assets/icons/云下载.svg" alt />
          </CButton>
        </template>
      </Table>
      <template #footer>
        <CButton
          @click="batchDownload"
          color="info"
          data-permission="Btn_QuaCheck_BatchD"
          >取消</CButton
        >
      </template>
    </Modal>
    <!-- </CModal> -->
  </div>
</template>
<script>
export default {
  name: "qualityReport",
  /* props: ["modalShow"],*/
  computed: {
    getColumnlist() {
      return [
        {
          type: "selection",
          width: 60,
          align: "center",
        },
        {
          key: "productName",
          title: "质检报告文件(文件夹)",
          render: (h, params) => {
            return h("div", [
              h(
                "span",
                {
                  style: {},
                  //就是这里加的属性
                  domProps: {
                    title: params.row.productName,
                  },
                },
                params.row.productName
              ),
            ]);
          },
        },
        {
          title: "操作",
          width: "150px",
          slot: "action",
          align: "center",
        },
      ];
    },
  },
  data() {
    return {
      modalStyle: { top: "340px" },
      selectionlist: [],
      qcdata: [],
      warningModal: true,
      modalShow: false,
    };
  },
  methods: {
    openOrClose() {
      this.modalShow = !this.modalShow;
    },
    closed() {
      this.$emit("closed");
    },
    handleSelectRow(selection, row) {
      this.selectionlist = [];
      for (let i = 0; i < selection.length; i++) {
        if (!selection[i].isFile) {
          this.$Message.warning("这是文件夹，请重新选择正确的文件");
          this.$nextTick(function () {
            // this.$set(
            //   this.$refs.selection.$refs.tbody.objData[i],
            //   "_isChecked",
            //   false
            // );//与下面一行效果相同
            this.$refs.selection.$refs.tbody.objData[i]._isChecked = false;
          });
        } else if (selection[i].isFile) {
          this.selectionlist.push(selection[i]);
        }
      }
    },
    //批量下载 是否留存
    batchDownload() {
      /*-------------修改段start-----------------*/
      //关闭弹框
      this.openOrClose();

      //批量下载
      /*let self = this;
      let lists = this.selectionlist;
      let newarr = [];

      for (let i = 0; i < lists.length; i++) {
        let tempData = lists[i];
        let temp =
          tempData.path === undefined ? tempData.productName : tempData.path;
        newarr.push(temp);
      }
      let paths = newarr;
      let path = this.GlobalUrlPath.qualityCheck.downloadReport + paths;
      window.open(path);*/

      /*-----------------修改段end------------------------*/
    },

    //单文件下载
    downloaddetail(row) {
      /*----------修改段start-------------*/
      //跳转到文件页面
      let that = this;
      let data = {
        path: this.$api.qualityCheckApi.fileIsExit, //this.GlobalUrlPath.downLoadFile.isExit,
        params: {
          username: this.$store.state.user.userName,
          path: row.path ? row.path : row.productName, //取值存在两种格式的行数据， 文件夹里面的文件路径取（path）和单独的文件取（productName）
          // path:"/pie/data/247/sthx_user_data/pieadmin/lilintest/123/GF3_KAS_QPSI_010970_E81.2_N40.5_20180910_L1A_VH_L10003442728.tif" //测试地址
          // path: "/pie/data/user/nwsj/11-SJCLXT/SJCL/user/pieadmin/xyz",
        },
      };
      // let fun = function () {
      //   that.$router.push({
      //     name: "MyFile",
      //     params: {
      //       type: "downLoadFile",
      //       dataList: data.params,
      //     },
      //   });
      // };
      let fun = function () {
        let url =
          "DownloadFile://" +
            sessionStorage.getItem("PIECloud-Token") +
          "/" +
          data.params.username +
          "/" +
          data.params.path +
          "/";
        window.location.href = url;
      };
      this.$$downLoadFile(data, fun);

      //弹框下载
      /*
          if (row.isFile) {
            let paths = row.path === undefined ? row.productName : row.path; //row.productName;
            let self = this;
            let path = this.GlobalUrlPath.qualityCheck.downloadReport + paths;
            window.open(path);
          } else {
            this.$Message.warning("这是文件夹，请选择正确的文件");
          }
          */
      /* --------------修改段end---------------- */
    },
    redaskDetails(row) {
      if (row.isFile) {
        let userLink = `/qualityCheck/infoPreview`;
        this.$router.push({
          path: userLink,
          query: {
            from: "quality",
            path: row.path === undefined ? row.productName : row.path,
          },
        });
      } else {
        this.getReportByPath(row.productName);
      }
    },
    getReportByPath(dirPath) {
      // this.loading = true;
      let self = this;
      // let path = this.GlobalUrlPath.processProduct.getFileListByPath;

      let path = this.$api.qualityCheckApi.getFileListByPath;
      let param = {
        path: dirPath,
        // _loadingContainer: document.querySelector(".container")
      };
      self.qcdata = [];
      this.http.get(path, param).then((res) => {
        if (res.data.code == 0 && res.data.success) {
          let data = res.data.data;
          for (let i = 0; i < data.length; i++) {
            let param = {};
            param.productName = data[i].name;
            param.path = data[i].path;
            param.isFile = data[i].isFile;
            self.qcdata.push(param);
          }
          // this.loading = false;
        } else {
          self.$Message.error(res.data.Message);
        }
      });
    },
    getTaskDetails(item, tag) {
      let self = this;
      let taskDetailId = item.id;
      if (taskDetailId == undefined) return;
      let path = "";

      if (tag === "parent") {
        // path = this.GlobalUrlPath.processProduct.taskDetailResult + taskDetailId;
        path = this.$api.qualityCheckApi.taskDetailResult + taskDetailId;
      } else {
        // path = this.GlobalUrlPath.processProduct.queryByTaskDetailId + taskDetailId;
        path = this.$api.qualityCheckApi.queryByTaskDetailId + taskDetailId;
      }
      let param = {
        productType: 1,
        // _loadingContainer: document.querySelector(".container"),
      };
      this.http.get(path, param).then((res) => {
        self.qcdata = [];
        if (res.data.code == 0 && res.data.success) {
          let data = res.data.data;
          for (let i = 0; i < data.length; i++) {
            if (data[i].isFile == 0) {
              data[i].isFile = true;
            } else {
              data[i].isFile = false;
            }
            self.qcdata.push(data[i]);
          }
        } else {
          self.$Message.error(res.data.Message);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.btnStyle {
  margin-right: -6px;
}
</style>
